package com.sysu.minsysu.zdkb;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.sysu.R;
import com.sysu.minsysu.zdkb.utils.Course;

public class SearchResultList extends ListActivity{
	
	public static final String COURSES = "courses";

	ListView mListView;
	ArrayList<Course> mCoursesList;
	BaseAdapter mAdapter;
	boolean[] mIsSelect;
	
	Button mCommitButton;
	Button mCancelButton;
	@Override
	public void onCreate( Bundle savedInstanceState ){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchresultlayout);
		
		Intent intent = getIntent();
		mListView = getListView();
		setupViews();
		
		mCoursesList = (ArrayList<Course>)intent.getSerializableExtra(COURSES);
		mIsSelect = new boolean[mCoursesList.size()];
		mAdapter = new CourseAdapter( this );
		setListAdapter( mAdapter );
	}

	private void setupViews(){
		mCancelButton = (Button)findViewById(R.id.cancel);
		mCommitButton = (Button)findViewById(R.id.commit);
		mCommitButton.setOnClickListener( new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ArrayList<Integer> result = new ArrayList<Integer>();
				for( int i=0; i<mIsSelect.length; i++ )
					if( mIsSelect[i] )
						result.add( mCoursesList.get(i).getId());
				Intent data = new Intent();
				data.putExtra("result", result);
				setResult(RESULT_OK, data);
				finish();
			}
		});
		mCancelButton.setOnClickListener( new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	}
	
	private class CourseAdapter extends BaseAdapter{

		LayoutInflater mLayoutInflater;
		public CourseAdapter( Context context ){
			mLayoutInflater = LayoutInflater.from(context);
		}
		
		@Override
		public int getCount() {
			return mCoursesList.size(); 
		}

		@Override
		public Object getItem(int position) {
			return mCoursesList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return mCoursesList.get(position).getId();
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if( convertView!= null )
				holder = (ViewHolder)convertView.getTag();
			else {
				holder = new ViewHolder();
				convertView = mLayoutInflater.inflate(R.layout.courseview, null);
				holder = new ViewHolder();
				holder.selectBox = (CheckBox) convertView.findViewById(R.id.selectBox);
				holder.courseNameTextView = (TextView) convertView.findViewById(R.id.courseName);
				holder.teacherNameTextView = (TextView) convertView.findViewById(R.id.teacherName);
				holder.timeTextView = (TextView) convertView.findViewById(R.id.courseTime);
				holder.showDetailButton = (Button) convertView.findViewById(R.id.showDetail);
				//convertView.setTag(holder);
			}
			final Course course = mCoursesList.get(position);
			holder.selectBox.setChecked( mIsSelect[position]);
			holder.selectBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					mIsSelect[position] = isChecked;
				}
			});
			
			holder.showDetailButton.setOnClickListener( new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					showCourseDetail(course);
				}
			});
			
			holder.courseNameTextView.setText(course.getCourseName());
			holder.teacherNameTextView.setText(course.getTeacherName());
			holder.timeTextView.setText( course.getDate() + " " + course.getClassInterval());

			return convertView;
			}
		}  
		
		private class ViewHolder{
			CheckBox selectBox;
			TextView courseNameTextView;
			TextView teacherNameTextView;
			TextView timeTextView;
			Button showDetailButton;
		}
		
	
	private void showCourseDetail( final Course course ){
		final DetailsDialog dialog = new DetailsDialog(SearchResultList.this, course );
		dialog.setSelectListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int t = mCoursesList.indexOf(course);
				mIsSelect[t] = true;
				mAdapter.notifyDataSetChanged();
				dialog.dismiss();
			}
		});
		dialog.show();
	}
}
